﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lenovo.CFI.BLL;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;

namespace Lenovo.CFI.Web.VP
{
    public partial class TopicList : System.Web.UI.UserControl
    {
        TopicBl topicBl = new TopicBl();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindData();

                this.BtnAdd.OnClientClick = string.Format("window.open('{0}')", UrlHelper.GenerateUrl("topicadd"));

                MessageHelper.Prepare(this.Page);
            }
        }

        

        private void BindData()
        {
            GvTopics.DataSource = topicBl.GetTopic();
            GvTopics.DataBind();
        }

        protected void GvTopics_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // 设置行号
                ((Literal)(e.Row.FindControl("LtrNo"))).Text = String.Format("{0}", e.Row.DataItemIndex + 1);

                LinkButton btnAction1 = (LinkButton)e.Row.FindControl("BtnPhaseAction1");
                LinkButton btnAction2 = (LinkButton)e.Row.FindControl("BtnPhaseAction2");
                LinkButton btnAction3 = (LinkButton)e.Row.FindControl("BtnPhaseAction3");

                Topic topic = (Topic)e.Row.DataItem;

                ((Literal)e.Row.FindControl("LtrTitle")).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1} {2}</a>",
                    UrlHelper.GenerateUrl("topicdetail", "id", topic.ID.ToString()), topic.No, topic.Title);

                switch (topic.Status)
                {
                    case TopicStatus.Create:
                        btnAction1.Text = "Start"; btnAction1.CommandName = "Start"; btnAction1.CommandArgument = topic.ID.ToString();
                        btnAction2.Text = "Edit"; btnAction2.OnClientClick = string.Format("window.open('{0}')", 
                            UrlHelper.GenerateUrl("topicedit", "id", topic.ID.ToString()));
                        btnAction3.Text = "Delete"; btnAction3.CommandName = "Remove"; btnAction3.CommandArgument = topic.ID.ToString();
                        break;
                    case TopicStatus.Start:
                        btnAction1.Text = "Stop";btnAction1.CommandName = "Stop"; btnAction1.CommandArgument = topic.ID.ToString();
                        btnAction2.Visible = false;
                        btnAction3.Visible = false;
                        break;
                    case TopicStatus.Schedule:
                        btnAction1.Text = "Set Schedule"; btnAction1.OnClientClick = string.Format("window.open('{0}')",
                            UrlHelper.GenerateUrl("topicschedule", "id", topic.ID.ToString()));
                        btnAction2.Visible = false;
                        btnAction3.Visible = false;
                        break;
                    case TopicStatus.Reviewer:
                        btnAction1.Text = "Set Reviewer"; btnAction1.OnClientClick = string.Format("window.open('{0}')",
                            UrlHelper.GenerateUrl("topicreviewer", "id", topic.ID.ToString()));
                        btnAction2.Visible = false;
                        btnAction3.Visible = false;
                        break;
                    case TopicStatus.Review:
                        btnAction1.Text = "ReviewList"; btnAction1.OnClientClick = string.Format("window.open('{0}')",
                            UrlHelper.GenerateUrl("topicreviewlist", "id", topic.ID.ToString()));
                        btnAction2.Text = "Record"; btnAction2.OnClientClick = string.Format("window.open('{0}')",
                            UrlHelper.GenerateUrl("topicrecord", "id", topic.ID.ToString()));
                        //btnAction3.Text = "Stop"; btnAction3.CommandName = "Summary"; btnAction3.CommandArgument = topic.ID.ToString();
                        btnAction3.Text = "Summary"; btnAction3.OnClientClick = string.Format("window.open('{0}')",
                            UrlHelper.GenerateUrl("topicsummary", "id", topic.ID.ToString()));
                        break;
                    case TopicStatus.Summary:
                        btnAction1.Text = "Summary"; btnAction1.OnClientClick = string.Format("window.open('{0}')",
                            UrlHelper.GenerateUrl("topicsummary", "id", topic.ID.ToString()));
                        btnAction2.Text = "Set DC&IP"; btnAction2.OnClientClick = string.Format("window.open('{0}')",
                            UrlHelper.GenerateUrl("topicdcip", "id", topic.ID.ToString()));
                        btnAction3.Text = "Suggestion"; btnAction3.CommandName = "Suggestion"; btnAction3.CommandArgument = topic.ID.ToString();
                        break;
                    case TopicStatus.Suggestion:
                        btnAction1.Text = "IdeaList"; btnAction1.OnClientClick = string.Format("window.open('{0}')",
                            UrlHelper.GenerateUrl("topicideastatus", "id", topic.ID.ToString()));
                        btnAction2.Text = "Execution"; btnAction2.CommandName = "Execution"; btnAction2.CommandArgument = topic.ID.ToString();
                        btnAction3.Visible = false;
                        break;
                    case TopicStatus.Execution:
                        btnAction1.Text = "IdeaList"; btnAction1.OnClientClick = string.Format("window.open('{0}')",
                            UrlHelper.GenerateUrl("topicideastatus", "id", topic.ID.ToString()));
                        btnAction2.Text = "Close"; btnAction2.CommandName = "Close"; btnAction2.CommandArgument = topic.ID.ToString();
                        btnAction3.Visible = false;
                        break;
                    case TopicStatus.Closed:
                        btnAction1.Text = "IdeaList"; btnAction1.OnClientClick = string.Format("window.open('{0}')",
                            UrlHelper.GenerateUrl("topicideastatus", "id", topic.ID.ToString()));
                        break;
                    default:
                        break;
                }
            }
        }


        protected void GvTopics_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Start")   // 进入启动
            {
                try
                {
                    this.topicBl.StartTopic(Convert.ToInt32(e.CommandArgument));

                    MessageHelper.RegShowJSAjax(this.GvTopics, "Success!", this.Page);
                }
                catch (ApplicationException aex)
                {
                    MessageHelper.RegShowJSAjax(this.GvTopics, aex.Message, this.Page);
                }
                catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
                {
                    ErrorHelper.ExceptionHanderForAjax(ex, this.GvTopics, this.Page);
                }

                this.BindData();
            }
            else if (e.CommandName == "Remove")   // 
            {
                try
                {
                    this.topicBl.RemoveTopic(Convert.ToInt32(e.CommandArgument));

                    MessageHelper.RegShowJSAjax(this.GvTopics, "Success!", this.Page);
                }
                catch (ApplicationException aex)
                {
                    MessageHelper.RegShowJSAjax(this.GvTopics, aex.Message, this.Page);
                }
                catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
                {
                    ErrorHelper.ExceptionHanderForAjax(ex, this.GvTopics, this.Page);
                }

                this.BindData();
            }
            else if (e.CommandName == "Stop")   // 
            {
                try
                {
                    this.topicBl.PrepareSchedule(Convert.ToInt32(e.CommandArgument));

                    MessageHelper.RegShowJSAjax(this.GvTopics, "Success!", this.Page);
                }
                catch (ApplicationException aex)
                {
                    MessageHelper.RegShowJSAjax(this.GvTopics, aex.Message, this.Page);
                }
                catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
                {
                    ErrorHelper.ExceptionHanderForAjax(ex, this.GvTopics, this.Page);
                }

                this.BindData();
            }
            else if (e.CommandName == "Summary")   // 
            {
                try
                {
                    this.topicBl.StartSummary(Convert.ToInt32(e.CommandArgument));

                    MessageHelper.RegShowJSAjax(this.GvTopics, "Success!", this.Page);
                }
                catch (ApplicationException aex)
                {
                    MessageHelper.RegShowJSAjax(this.GvTopics, aex.Message, this.Page);
                }
                catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
                {
                    ErrorHelper.ExceptionHanderForAjax(ex, this.GvTopics, this.Page);
                }

                this.BindData();
            }
            else if (e.CommandName == "Suggestion")   // 
            {
                Topic topic = topicBl.GetTopicByID(Convert.ToInt32(e.CommandArgument));

                this.DpFeedbackSuggestion.Value = topic.SuggestDate.Value.ToString("yyyy-MM-dd");
                this.DpSubmitSuggestion.Value = topic.MaterialDate.Value.ToString("yyyy-MM-dd");
                this.DpGmoSuggestion.Value = topic.ReportDate.Value.ToString("yyyy-MM-dd");

                this.MpeSuggestion.Show();

                ViewState["tid"] = Convert.ToInt32(e.CommandArgument);
            }
            else if (e.CommandName == "Execution")   // 
            {
                Topic topic = topicBl.GetTopicByID(Convert.ToInt32(e.CommandArgument));

                this.DpSubmitExecution.Value = topic.MaterialDate.Value.ToString("yyyy-MM-dd");
                this.DpReportExecutionMkt.Value = topic.MktDate.Value.ToString("yyyy-MM-dd");
                this.DpGmoExecution.Value = topic.ReportDate.Value.ToString("yyyy-MM-dd");
                this.TxtSubmitTo.Text = topic.Principal;

                this.MpeExecution.Show();

                ViewState["tid"] = Convert.ToInt32(e.CommandArgument);
            }
            else if (e.CommandName == "Close")   // 
            {
                try
                {
                    this.topicBl.CloseTopic(Convert.ToInt32(e.CommandArgument));

                    MessageHelper.RegShowJSAjax(this.GvTopics, "Success!", this.Page);
                }
                catch (ApplicationException aex)
                {
                    MessageHelper.RegShowJSAjax(this.GvTopics, aex.Message, this.Page);
                }
                catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
                {
                    ErrorHelper.ExceptionHanderForAjax(ex, this.GvTopics, this.Page);
                }

                this.BindData();
            }
        }

        protected void BtnSaveSuggestion_Click(object sender, EventArgs e)
        {
            try
            {
                this.topicBl.StartSuggestion((int)ViewState["tid"], this.DpFeedbackSuggestion.Date.Value,
                    this.DpSubmitSuggestion.Date.Value, this.DpGmoSuggestion.Date.Value);

                MessageHelper.RegShowJSAjax(this.BtnSaveSuggestion, "Success!", this.Page);
            }
            catch (ApplicationException aex)
            {
                MessageHelper.RegShowJSAjax(this.BtnSaveSuggestion, aex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.ExceptionHanderForAjax(ex, this.BtnSaveSuggestion, this.Page);
            }

            this.BindData();
        }

        protected void BtnSaveExecution_Click(object sender, EventArgs e)
        {
            try
            {
                this.topicBl.StartExecution((int)ViewState["tid"], this.DpGmoExecution.Date.Value,
                    this.DpReportExecutionMkt.Date.Value, this.DpSubmitExecution.Date.Value, 
                    this.TxtSubmitTo.Text);

                MessageHelper.RegShowJSAjax(this.BtnSaveExecution, "Success!", this.Page);
            }
            catch (ApplicationException aex)
            {
                MessageHelper.RegShowJSAjax(this.BtnSaveExecution, aex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.ExceptionHanderForAjax(ex, this.BtnSaveExecution, this.Page);
            }

            this.BindData();
        }
    }
}